//
//  RecommendView.m
//  糗事百科
//
//  Created by 奚超明 on 16/6/3.
//  Copyright © 2016年 奚超明. All rights reserved.
//

#import "RecommendView.h"

@interface RecommendView () <UITextViewDelegate>

@end

@implementation RecommendView

- (instancetype)initWithFrame:(CGRect)frame target:(id)target expressionAction:(SEL)expressionAction enterAction:(SEL)enterAction
{
    if (self == [super initWithFrame:frame]) {
        // 背景色
        self.backgroundColor = [UIColor whiteColor];
        
        // 表情
        UIButton *expressionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [expressionBtn addTarget:target action:expressionAction forControlEvents:UIControlEventTouchUpInside];
        [expressionBtn setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
        [self addSubview:expressionBtn];
        self.expressionBtn = expressionBtn;
        
        // 输入框
        UITextView *textView = [[UITextView alloc] init];
        textView.delegate = self;
//        textView.scrollEnabled = NO;
        textView.font = [UIFont systemFontOfSize:13];
        textView.backgroundColor = kBackGroudColor;
        textView.layer.cornerRadius = 5;
        textView.layer.masksToBounds = YES;
        [self addSubview:textView];
        self.textView = textView;
        
        // 确定评论
        UIButton *enterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        enterBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [enterBtn addTarget:target action:enterAction forControlEvents:UIControlEventTouchUpInside];
        [enterBtn setTitleColor:kOrangerColor forState:UIControlStateNormal];
        [enterBtn setTitle:@"评论" forState:UIControlStateNormal];
        [self addSubview:enterBtn];
        self.enterBtn = enterBtn;
        
        // 分割线
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = [UIColor lightGrayColor];
        line.alpha = 0.5;
        [self addSubview:line];
        self.line = line;
    }
    return self;
}

- (void)layoutSubviews
{
    CGFloat expressionBtnWidth = 30;
    CGFloat expressionBtnHeight = expressionBtnWidth;
    CGFloat expressionBtnX = kMargin * 0.5;
    CGFloat expressionBtnY = self.xcm_height - kMargin * 0.5 - expressionBtnHeight;
    self.expressionBtn.frame = CGRectMake(expressionBtnX, expressionBtnY, expressionBtnWidth, expressionBtnHeight);
    
    CGFloat enterBtnWidth = 40;
    CGFloat enterBtnHeight = expressionBtnHeight;
    CGFloat enterBtnX = self.xcm_width - kMargin * 0.5 - enterBtnWidth;
    CGFloat enterBtnY = expressionBtnY;
    self.enterBtn.frame = CGRectMake(enterBtnX, enterBtnY, enterBtnWidth, enterBtnHeight);
    
    CGFloat textViewHeight = expressionBtnHeight;
    CGFloat textViewWidth = self.xcm_width - 2 * kMargin - expressionBtnWidth - enterBtnWidth;
    CGFloat textViewX = expressionBtnWidth + kMargin;
    CGFloat textViewY = self.xcm_height - kMargin * 0.5 - textViewHeight;
    if (self.textView.xcm_height == 0) {
        self.textView.frame = CGRectMake(textViewX, textViewY, textViewWidth, textViewHeight);
    } else {
        CGFloat textViewHeight = self.textView.contentSize.height;
        CGFloat textViewY = self.xcm_height - kMargin * 0.5 - textViewHeight;
        self.textView.frame = CGRectMake(textViewX, textViewY, textViewWidth, textViewHeight);
    }
    
    
    self.line.frame = CGRectMake(0, 0, self.xcm_width, 1);
}

#pragma mark - 内容改变自适应高度
- (void)textViewDidChange:(UITextView *)textView
{
    CGFloat height = textView.contentSize.height;
    
    if (height != textView.xcm_height) {
        textView.xcm_height = height;
        
        // 将textView的高度传出去
        if (self.block != nil) {
            self.block(textView.xcm_height);
        }
    }
}

@end
